<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="院校名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="院校名称"></el-input>
      </el-form-item>
      <el-form-item label="院校简介" prop="info">
        <el-input v-model="dataForm.info" type="textarea" placeholder="院校简介"></el-input>
      </el-form-item>
      <el-form-item label="院校地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="院校地址"></el-input>
      </el-form-item>
      <el-form-item label="学生人数" prop="sum">
        <el-input-number v-model="dataForm.sum" :min="1" label="学生人数"></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        roleList: [],
        dataForm: {
          id: 0,
          name: '',
          info: '',
          address: '',
          sum: ''
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
        this.$http({
          url: this.$http.adornUrl(`/sys/sysSchool/select`),
          method: 'get',
          params: this.$http.adornParams(
            {id: this.dataForm.id},false
          )
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataForm.id = data.data.id
            this.dataForm.name = data.data.name
            this.dataForm.info = data.data.info
            this.dataForm.address = data.data.address
            this.dataForm.sum = data.data.sum
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            console.log(this.dataForm);
            let url = this.dataForm.id === 0 ? '/sys/sysSchool/addSchool' : '/sys/sysSchool/updateSchool'
            this.$http({
              url: this.$http.adornUrl(`/sys/sysSchool/${this.dataForm.id == 0 ? 'addSchool' : 'updateSchool'}`),
              method: 'post',
              data: this.$http.adornData(
                {
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'info': this.dataForm.info,
                'address': this.dataForm.address,
                'sum': this.dataForm.sum,
                },
                false
              )
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
